#!/bin/bash
## Generate content for github.com/ehcache/ehcache.github.io
##
## This script presumes:
## 1) ehcache/ehcache.github.io is checked out and available locally
## 2) this script is being executed from it's "home" directory (the 'docs' directory of the ehcache3 project)

declare -r syntax="Syntax: ${0} <ehcache.github.io_project_root> <version>"

declare -r contentRoot="$( cd "$(dirname "${0}")" ; pwd )"
declare -r siteDocDir="${contentRoot}/build/asciidoc/user"

## 'adoc' files which don't need direct conversion
declare -ar adocExclusions=(
    common.adoc
    menu.adoc
)

if [ ! -d "${siteDocDir}" ]; then
    echo "Directory '${siteDocDir}' not found" >&2
    exit 1
fi

## Take the first  argument as the location of the ehcache.github.io project root
if [ "${1}" == "" ]; then
    echo "Location of ehcache/ehcache.github.io project root must be provided" >&2
    echo "${syntax}" >&2
    exit 2
fi

if [ ! -d "${1}" ]; then
    echo "Directory '${1}' not found" >&2
    exit 1
fi
declare -r siteRoot="$(cd "${1}" ; pwd)"

if [ "${2}" == "" ]; then
    echo "The version identifier must be provided" >&2
    echo "${syntax}" >&2
    exit 2
fi
declare -r version="${2}"

## Confirm replacement if content already exists
declare -r docsTarget="${siteRoot}/docs/user/${version}"
if [ -e "${docsTarget}/ehcache.css" ]; then
    read -p "Content exists at '${docsTarget}'; replace? (y|n)"
    if [ "${REPLY}" != "y" ]; then
        echo "Abandoning web site document installation" >&2
        exit 3
    fi
    echo "Replacing content at '${docsTarget}'"
else
    echo "Installing content to '${docsTarget}'"
fi

## 0) If target directory does not exist, create it
if [ ! -d "${docsTarget}" ]; then
    mkdir -p "${docsTarget}" || exit
fi

## 1) Convert and place
echo "Converting & installing adocs"
../gradlew clean asciidoctor || exit
cp -r -p "${siteDocDir}/"* "${docsTarget}/." || exit

echo "Done"

    



